//
//  AppDelegate.swift
//  swiftDemo
//
//  Created by luozhilin on 2018/9/7.
//  Copyright © 2018年 luozhilin. All rights reserved.
//

import UIKit
import ESTabBarController_swift

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        
        let tab = self.setupTabBar(delegate: self as? UITabBarControllerDelegate)
        self.window?.rootViewController = tab
        window?.backgroundColor = UIColor.white
        window?.makeKeyAndVisible()
        self.compareCurrentVersionWithOld();
        return true
    }

    func compareCurrentVersionWithOld() {
        
        let alertVC = UIAlertController(title: "更新提示", message: nil, preferredStyle: .alert)
        let cancelAction = UIAlertAction(title: "取消", style: .cancel, handler: nil)
        let sureAction = UIAlertAction(title: "确定", style: .default, handler: nil)
        
        alertVC.addAction(cancelAction)
        alertVC.addAction(sureAction)
        
        guard let lastVersion = AppDelegate.lastVersion() else {
            
            UserDefaults.standard.set(AppDelegate.localVersion(), forKey: "appVersion")
            
            return
        }
        
        let showAlert = AppDelegate.versionCompare(localVersion: lastVersion, serverVersion: AppDelegate.localVersion())
        
        if showAlert {

            if let vc = UIApplication.shared.keyWindow?.rootViewController {
                
                self.setStaticGuidePage();
                
                UserDefaults.standard.set(AppDelegate.localVersion(), forKey: "appVersion")
                UserDefaults.standard.synchronize()
            }
        }
    }
    
    // 版本比较
    private static func versionCompare(localVersion: String, serverVersion: String) -> Bool {
        let result = localVersion.compare(serverVersion, options: .numeric, range: nil, locale: nil)
        if result == .orderedDescending || result == .orderedSame{
            return false
        }
        return true
    }
    
    //本地版本
    private static func localVersion() -> String! {
        
        return Bundle.main.infoDictionary!["CFBundleShortVersionString"] as! String
    }
    
    //上一次版本
    private static func lastVersion() -> String? {
        
        return UserDefaults.standard.object(forKey: "appVersion") as? String
    }
    
    func applicationWillResignActive(_ application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(_ application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(_ application: UIApplication) {
        // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(_ application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(_ application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }

    func setStaticGuidePage() {
        let imageNameArray: [String] = ["lead01", "lead02", "lead03"]
        let guideView = LZLNewFeatureViewController.init(imageWithArray:imageNameArray, isHiddenSkipBtn:false)
        self.window?.rootViewController?.view.addSubview(guideView)
    }
    
    func setupTabBar(delegate:UITabBarControllerDelegate?) -> ESTabBarController {
        
        let tabBarController = ESTabBarController()
        tabBarController.delegate = delegate
        tabBarController.title = "Irregularity"
        tabBarController.tabBar.shadowImage = UIImage(named: "transparent")
        tabBarController.shouldHijackHandler = {
            tabbarController, viewController, index in
            if index == 2 {
                return true
            }
            return false
        }
        let v1 = LZLHomePageViewController()
        let v2 = LZLListenViewController()
        let v3 = LZLPlayerViewController()
        let v4 = LZLFindViewController()
        let v5 = LZLMyViewController()
        
        v1.tabBarItem = ESTabBarItem.init(YYIrregularityBasicContentView(), title: "首页", image: UIImage(named: "home"), selectedImage: UIImage(named: "home_1"))
        v2.tabBarItem = ESTabBarItem.init(YYIrregularityBasicContentView(), title: "我听", image: UIImage(named: "find"), selectedImage: UIImage(named: "find_1"))
        v3.tabBarItem = ESTabBarItem.init(YYIrregularityContentView(), title: nil, image: UIImage(named: "tab_play"), selectedImage: UIImage(named: "tab_play"))
        v4.tabBarItem = ESTabBarItem.init(YYIrregularityBasicContentView(), title: "发现", image: UIImage(named: "favor"), selectedImage: UIImage(named: "favor_1"))
        v5.tabBarItem = ESTabBarItem.init(YYIrregularityBasicContentView(), title: "我的", image: UIImage(named: "me"), selectedImage: UIImage(named: "me_1"))
        let n1 = YYNavigationController.init(rootViewController: v1)
        let n2 = YYNavigationController.init(rootViewController: v2)
        let n3 = YYNavigationController.init(rootViewController: v3)
        let n4 = YYNavigationController.init(rootViewController: v4)
        let n5 = YYNavigationController.init(rootViewController: v5)
        v1.title = "首页"
        v2.title = "我听"
        v3.title = "播放"
        v4.title = "发现"
        v5.title = "我的"
        
        tabBarController.viewControllers = [n1, n2, n3, n4, n5]
        tabBarController.selectedIndex = 3
        
        return tabBarController;
        
    }

}


